package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomException;
import com.itheima.dto.DishDto;
import com.itheima.entity.Dish;
import com.itheima.entity.DishFlavor;
import com.itheima.entity.Setmeal;
import com.itheima.entity.SetmealDish;
import com.itheima.mapper.DishMapper;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@Slf4j
@Service
@Transactional
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealService setmealService;

    /**
     * 添加新菜品
     * @param dishDto
     */
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        this.save(dishDto);
        Long dishId = dishDto.getId();//菜品id
        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据id查询菜品信息和对应的口味信息
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息，从dish表查询
        Dish dish = this.getById(id);

        //查询当前菜品对应的口味信息，从dish_flavor表查询
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(qw);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        dishDto.setFlavors(flavors);

        return dishDto;
    }

    /**
     * 修改菜品
     * @param dishDto
     * @return
     */
    @Override
    public boolean updateWithFlavor(DishDto dishDto) {
        //        1.菜品基本字段
        this.updateById(dishDto);
        //        2.修改菜品口味信息
        //            删除+新增=修改
        //        2.1 删除这个菜原来的口味
        //        DELETE FROM dish_flavor WHERE dish_id = ?;
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(qw);

        //        2.2 新增上新的口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        log.info("flavors:{}",flavors);
        //        口味dishid 赋值
        flavors = flavors.stream().map(item->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);

        return true;
    }

    /**
     * 根据id批量删除菜品
     * @param ids
     * @return
     */
    @Override
    public boolean deleteByIdsWithFlavor(List<Long> ids) {
        //判断哪个套餐中含有此菜品
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SetmealDish::getDishId,ids);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        if(list.size()>0){
            //有套餐含有其中id  得到套餐名字
            ArrayList<Long> setmealIds = new ArrayList<>(); //存储套餐id  去套餐表查询套餐名
            for (SetmealDish setmealDish : list) {
                setmealIds.add(setmealDish.getSetmealId());
            }
            List<Setmeal> setmeals = setmealService.listByIds(setmealIds);
            ArrayList<String> setmealName = new ArrayList<>();
            for (Setmeal setmeal : setmeals) {
                setmealName.add(setmeal.getName());
            }
            throw new CustomException("套餐"+setmealName+"含有删除菜品,无法删除");
        }

        //删除菜品表中的信息
        this.removeByIds(ids);
        //删除菜品封装类表中的信息
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(wrapper);
        return true;
    }
}
